package com.hongxun.design.acceptance.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.acceptance.mode.Child;
import com.hongxun.design.acceptance.mode.GroupTitle;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.ArrayList;

/**
 * Created by cqcqwu on 2018/03/12.
 */

public class ExLvAdapter extends BaseExpandableListAdapter {
    private Activity mActivity;
    private ArrayList<GroupTitle> groups;
    private Handler handler;
    private String nameValue = "";
    private int index = -1;

    /**
     * 上下文 mContext
     * 群组名 groups
     * 群组下的子项 childs
     */
    public ExLvAdapter(Object... obj) {
        this.mActivity = (Activity) obj[0];
        this.groups = (ArrayList<GroupTitle>) obj[1];
        this.handler = (Handler) obj[2];
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groups.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = LayoutInflater.from(mActivity).inflate(R.layout.groupview, null);
        TextView tl = (TextView) v.findViewById(R.id.tv_title_l);
        final TextView tr = (TextView) v.findViewById(R.id.tv_title_r);
        TextView tqh = (TextView) v.findViewById(R.id.tv_qiehuan);

        final LinearLayout layss = (LinearLayout) v.findViewById(R.id.lay_ss);
        final EditText name = (EditText) v.findViewById(R.id.serch_words);
        TextView search = (TextView) v.findViewById(R.id.tv_search);
        TextView ss = (TextView) v.findViewById(R.id.tv_ss);
        GroupTitle group = (GroupTitle) getGroup(groupPosition);
        tl.setText(group.getTitlel());
        tr.setText(group.getTitlec());
        tqh.setText(group.getTitler());
        if (group.getTitlel().equals(MString.existProblem)) {
            layss.setVisibility(View.GONE);
            ss.setVisibility(View.GONE);
            tqh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handler.sendEmptyMessage(4);
                }
            });
        } else if (group.getTitlel().equals(MString.problemLibrary)) {
            ss.setVisibility(View.GONE);
            layss.setVisibility(View.VISIBLE);
            tqh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handler.sendEmptyMessage(3);
                }
            });
            /*问题：EditText：第一次单击未获取焦点，关闭软键盘失去输入值*/
            //1、设置edittext的输入改变监听
            name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    nameValue = String.valueOf(s);
                }
            });
            //记录光标 焦点位置
            name.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        index = groupPosition;
                    }
                    return false;
                }
            });
            name.clearFocus();
            //重置焦点
            if (index != -1 && index == groupPosition) {
                name.requestFocus();
            }
            //重置光标
            name.setText(nameValue);
            name.setSelection(name.getText().toString().length());
            //搜索按钮
            search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String vname = String.valueOf(name.getText().toString().trim());
                    Message msg = handler.obtainMessage();
                    msg.obj = vname;
                    msg.what = 5;
                    handler.sendMessage(msg);
                }
            });
        }
        return v;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = LayoutInflater.from(mActivity).inflate(R.layout.groupview_item, null);
        TextView name = (TextView) v.findViewById(R.id.tv_proname);
        TextView type = (TextView) v.findViewById(R.id.tv_protype);
        TextView add = (TextView) v.findViewById(R.id.tv_add);
        ImageView tl = (ImageView) v.findViewById(R.id.iv_temp);

        TextView tvt = (TextView) v.findViewById(R.id.tv_t);
        TextView tvb = (TextView) v.findViewById(R.id.tv_b);

        final Child child = (Child) getChild(groupPosition, childPosition);
        tl.setImageURI(UriUtil.getUri(child.getUrl()));
        switch (child.getOprtype()) {
            case MString.existProblem:
                tvt.setVisibility(View.VISIBLE);
                tvb.setVisibility(View.VISIBLE);
                tvt.setText("存在问题：");
                tvb.setText("发现时间：");
                String findTime = child.getFindTime();
                type.setText(findTime + "\t" + child.getCoPro());
                name.setText(child.getExPro());
                if (child.getCoPro().equals("已验收")) {
                    add.setVisibility(View.GONE);
                    add.setOnClickListener(null);
                } else {
                    add.setVisibility(View.VISIBLE);
                    add.setText("验收");
                    add.setTextColor(mActivity.getResources().getColor(R.color.white));
                    add.setBackgroundColor(mActivity.getResources().getColor(R.color.caikan_green));
                    add.setOnClickListener(new MClickListener(childPosition, child) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            child.setCoPro("已验收");
                            notifyDataSetChanged();
                            Message message = handler.obtainMessage();
                            message.what = 9;
                            message.obj = msg;
                            message.arg1 = position;
                            handler.sendMessage(message);
                        }
                    });
                }
                break;
            case MString.problemLibrary:
                tvt.setVisibility(View.GONE);
                tvb.setVisibility(View.GONE);
//                tvt.setText("常见问题：");
//                tvb.setText("问题分类：");
                type.setText(child.getType());
                name.setText(child.getCoPro());
                add.setText("添加");
                add.setTextColor(mActivity.getResources().getColor(R.color.white));
                add.setBackgroundColor(mActivity.getResources().getColor(R.color.login_yellow));
                add.setOnClickListener(new MClickListener(childPosition, child) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        groups.get(1).getList().remove(position);
                        notifyDataSetChanged();
                        Message msg0 = handler.obtainMessage();
                        msg0.what = 8;
                        msg0.obj = msg;
                        msg0.arg1 = position;
                        handler.sendMessage(msg0);
                    }
                });
                break;
        }
        return v;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
